﻿/*
 * @author: wizardc
 */

using UnityEngine;

namespace Dou.Extend
{
    /// <summary>
    /// Transform 方法扩展
    /// </summary>
    public static class TransformExtend
    {
        public static string GetHierarchyPath(this Transform transform, Transform root = null)
        {
            return GetHierarchPathLoop(transform, root);
        }

        public static string GetHierarchyPath(this GameObject gameObject, Transform root = null)
        {
            return gameObject.transform.GetHierarchyPath(root);
        }

        public static string GetHierarchyPath(this Component component, Transform root = null)
        {
            return component.transform.GetHierarchyPath(root);
        }

        private static string GetHierarchPathLoop(Transform transform, Transform root = null, string path = null)
        {
            if (string.IsNullOrEmpty(path))
            {
                path = transform.name;
            }
            else
            {
                path = transform.gameObject.name + "/" + path;
            }
            if (transform.parent != null)
            {
                return GetHierarchPathLoop(transform.parent, root, path);
            }
            return path;
        }
    }
}
